/*     */ package com.zimbra.cs.account.ldap;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.AttributeManager;
/*     */ import com.zimbra.cs.account.AttributeManager.IDNType;
/*     */ import com.zimbra.cs.account.EntrySearchFilter;
/*     */ import com.zimbra.cs.account.EntrySearchFilter.Multi;
/*     */ import com.zimbra.cs.account.EntrySearchFilter.Operator;
/*     */ import com.zimbra.cs.account.EntrySearchFilter.Single;
/*     */ import com.zimbra.cs.account.EntrySearchFilter.Term;
/*     */ import com.zimbra.cs.account.EntrySearchFilter.Visitor;
/*     */ import com.zimbra.cs.account.IDNUtil;
/*     */ import com.zimbra.cs.ldap.ZLdapFilterFactory;
/*     */ import java.util.List;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class LdapEntrySearchFilter
/*     */ {
/*     */   public static class LdapQueryVisitor
/*     */     implements EntrySearchFilter.Visitor
/*     */   {
/*     */     private StringBuilder mLdapFilter;
/*  66 */     private boolean valueIsRaw = true;
/*     */     
/*     */     public LdapQueryVisitor() {
/*  69 */       this(true);
/*     */     }
/*     */     
/*     */     public LdapQueryVisitor(boolean valueIsRaw) {
/*  73 */       this.mLdapFilter = new StringBuilder();
/*  74 */       this.valueIsRaw = valueIsRaw;
/*     */     }
/*     */     
/*     */     public String getFilter() {
/*  78 */       return this.mLdapFilter.toString();
/*     */     }
/*     */     
/*     */     public void visitSingle(EntrySearchFilter.Single term) {
/*  82 */       EntrySearchFilter.Operator op = term.getOperator();
/*  83 */       boolean negation = term.isNegation();
/*     */       
/*     */ 
/*  86 */       if (op.equals(EntrySearchFilter.Operator.gt)) {
/*  87 */         op = EntrySearchFilter.Operator.le;
/*  88 */         negation = !negation;
/*  89 */       } else if (op.equals(EntrySearchFilter.Operator.lt)) {
/*  90 */         op = EntrySearchFilter.Operator.ge;
/*  91 */         negation = !negation;
/*     */       }
/*     */       
/*  94 */       if (negation) {
/*  95 */         this.mLdapFilter.append("(!");
/*     */       }
/*     */       
/*  98 */       ZLdapFilterFactory filterFactory = ZLdapFilterFactory.getInstance();
/*  99 */       String filter = null;
/*     */       
/* 101 */       String attr = term.getLhs();
/* 102 */       String val = getVal(term);
/* 103 */       if (op.equals(EntrySearchFilter.Operator.has)) {
/* 104 */         filter = filterFactory.substringFilter(attr, val, this.valueIsRaw);
/* 105 */       } else if (op.equals(EntrySearchFilter.Operator.eq))
/*     */       {
/* 107 */         if (val.equals("*")) {
/* 108 */           filter = filterFactory.presenceFilter(attr);
/*     */         } else {
/* 110 */           filter = filterFactory.equalityFilter(attr, val, this.valueIsRaw);
/*     */         }
/* 112 */       } else if (op.equals(EntrySearchFilter.Operator.ge)) {
/* 113 */         filter = filterFactory.greaterOrEqualFilter(attr, val, this.valueIsRaw);
/* 114 */       } else if (op.equals(EntrySearchFilter.Operator.le)) {
/* 115 */         filter = filterFactory.lessOrEqualFilter(attr, val, this.valueIsRaw);
/* 116 */       } else if (op.equals(EntrySearchFilter.Operator.startswith)) {
/* 117 */         filter = filterFactory.startsWithFilter(attr, val, this.valueIsRaw);
/* 118 */       } else if (op.equals(EntrySearchFilter.Operator.endswith)) {
/* 119 */         filter = filterFactory.endsWithFilter(attr, val, this.valueIsRaw);
/*     */       }
/*     */       else {
/* 122 */         filter = filterFactory.equalityFilter(attr, val, this.valueIsRaw);
/*     */       }
/*     */       
/* 125 */       this.mLdapFilter.append(filter);
/*     */       
/* 127 */       if (negation) {
/* 128 */         this.mLdapFilter.append(')');
/*     */       }
/*     */     }
/*     */     
/*     */     public void enterMulti(EntrySearchFilter.Multi term) {
/* 133 */       if (term.isNegation()) this.mLdapFilter.append("(!");
/* 134 */       if (term.getTerms().size() > 1) {
/* 135 */         if (term.isAnd()) {
/* 136 */           this.mLdapFilter.append("(&");
/*     */         } else
/* 138 */           this.mLdapFilter.append("(|");
/*     */       }
/*     */     }
/*     */     
/*     */     public void leaveMulti(EntrySearchFilter.Multi term) {
/* 143 */       if (term.getTerms().size() > 1) this.mLdapFilter.append(')');
/* 144 */       if (term.isNegation()) this.mLdapFilter.append(')');
/*     */     }
/*     */     
/*     */     protected String getVal(EntrySearchFilter.Single term)
/*     */     {
/* 149 */       return term.getRhs();
/*     */     }
/*     */   }
/*     */   
/*     */   private static class LdapQueryVisitorIDN extends LdapEntrySearchFilter.LdapQueryVisitor implements EntrySearchFilter.Visitor
/*     */   {
/*     */     private LdapQueryVisitorIDN() {
/* 156 */       super();
/*     */     }
/*     */     
/*     */     protected String getVal(EntrySearchFilter.Single term) {
/* 160 */       String rhs = term.getRhs();
/*     */       
/* 162 */       AttributeManager attrMgr = null;
/*     */       try {
/* 164 */         attrMgr = AttributeManager.getInstance();
/*     */       } catch (ServiceException e) {
/* 166 */         ZimbraLog.account.warn("failed to get AttributeManager instance", e);
/*     */       }
/*     */       
/* 169 */       AttributeManager.IDNType idnType = AttributeManager.idnType(attrMgr, term.getLhs());
/* 170 */       rhs = IDNUtil.toAscii(rhs, idnType);
/*     */       
/* 172 */       return rhs;
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static String toLdapIDNFilter(String filterStr)
/*     */   {
/*     */     String asciiQuery;
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */     try
/*     */     {
/* 196 */       EntrySearchFilter.Term term = LdapFilterParser.parse(filterStr);
/* 197 */       EntrySearchFilter filter = new EntrySearchFilter(term);
/* 198 */       asciiQuery = toLdapIDNFilter(filter);
/* 199 */       ZimbraLog.account.debug("original query=[" + filterStr + "], converted ascii query=[" + asciiQuery + "]");
/*     */     } catch (ServiceException e) {
/* 201 */       ZimbraLog.account.warn("unable to convert query to ascii, using original query: " + filterStr, e);
/* 202 */       asciiQuery = filterStr;
/*     */     }
/*     */     
/* 205 */     return asciiQuery;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private static String toLdapIDNFilter(EntrySearchFilter filter)
/*     */     throws ServiceException
/*     */   {
/* 218 */     LdapQueryVisitor visitor = new LdapQueryVisitorIDN(null);
/* 219 */     filter.traverse(visitor);
/* 220 */     return visitor.getFilter();
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static String toLdapCalendarResourcesFilter(EntrySearchFilter filter)
/*     */     throws ServiceException
/*     */   {
/* 230 */     LdapQueryVisitor visitor = new LdapQueryVisitor();
/* 231 */     filter.traverse(visitor);
/* 232 */     return visitor.getFilter();
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/account/ldap/LdapEntrySearchFilter.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */